#include<iostream>
using namespace std;

int main() {
    cout << "请输入25个字符，默认是从左到右，从上到下，每个格子的字母， 空格用0表示" << endl;
    string s;
    cin >> s;
    int arr[5][5];
    int xpos = 0, ypos = 0;
//    判断是否超过界限
    if (s.length() != 25) {
        cout << s.length();
        cout << "字符串的长度不对，请重新输入" << endl;
        return -1;
    } else {
//        初始化每一个位子
        for (int i = 0; i != 5; i++) {
            for (int j = 0; j != 5; j++) {
                arr[i][j] = char(s[i * 5 + j]);
                if ( arr[i][j] == '0') {
                    xpos = i;
                    ypos = j;
                }
            }
        }
        for (int i = 0; i != 5; i++) {
            for (int j = 0; j != 5; j++) {
                cout << char(arr[i][j]) << " ";
            }
            cout << endl;
        }
         cout << "运动后" << endl;
    }
    cout << "请输出移动规则" << endl;
    string direction;
    cin >> direction;
    int len = int(direction.length());
    for (int i = 0; i != len; i++) {
        switch (direction[i]) {
            case 'A':
            case 'a':
                if (xpos - 1 <0) {
                    cout << "越界"<< endl;
                    return -1;
                } else {
                    int temp = arr[xpos][ypos];
                    arr[xpos][ypos] = arr[xpos-1][ypos];
                    arr[xpos - 1][ypos] = temp;
                    xpos = xpos - 1;
                }
                for (int i = 0; i != 5; i++) {
                    for (int j = 0; j != 5; j++) {
                        cout << char(arr[i][j]) << " ";
                    }
                    cout << endl;
                }
                cout << "运动后" << endl;
                break;
            case 'B':
            case 'b':
                if (xpos + 1 > 4) {
                    cout << "越界"<< endl;
                    return -1;
                } else {
                    int temp = arr[xpos][ypos];
                    arr[xpos][ypos] = arr[xpos+1][ypos];
                    arr[xpos + 1][ypos] = temp;
                    xpos = xpos + 1;
                }
                for (int i = 0; i != 5; i++) {
                    for (int j = 0; j != 5; j++) {
                        cout << char(arr[i][j]) << " ";
                    }
                    cout << endl;
                }
                 cout << "运动后" << endl;
                break;
            case 'L':
            case 'l':
                if (ypos - 1 <0) {
                    cout << "越界"<< endl;
                    return -1;
                } else {
                    int temp = arr[xpos][ypos];
                    arr[xpos][ypos] = arr[xpos][ypos - 1];
                    arr[xpos][ypos - 1] = temp;
                    ypos = ypos - 1;
                }
                for (int i = 0; i != 5; i++) {
                    for (int j = 0; j != 5; j++) {
                        cout << char(arr[i][j]) << " ";
                    }
                    cout << endl;
                }
                cout << "运动后" << endl;
                break;
            case 'R':
            case 'r':
                if (ypos + 1 > 4) {
                    cout << "越界"<< endl;
                    return -1;
                } else {
                    int temp = arr[xpos][ypos];
                    arr[xpos][ypos] = arr[xpos][ypos + 1];
                    arr[xpos][ypos + 1] = temp;
                    ypos = ypos+1;
                }
                for (int i = 0; i != 5; i++) {
                    for (int j = 0; j != 5; j++) {
                        cout << char(arr[i][j]) << " ";
                    }
                    cout << endl;
                }
                cout << "运动后" << endl;
                break;
            default:
                cout << "结束" << endl;
                for (int i = 0; i != 5; i++) {
                    for (int j = 0; j != 5; j++) {
                        cout << char(arr[i][j]) << " ";
                    }
                    cout << endl;
                }
                cout << "运动后" << endl;
                break;
        }
    }
    return 0;
}
